import 'dart:convert';

import 'base_model.dart';

enum CommentType { officialContent, ceramic }

class CommentModel implements BaseModel {
  final String id;
  final String content;
  final String userId;
  final String userName;
  final String userAvatar;
  final String contentId;
  final CommentType type;
  final int likeCount;
  final DateTime createdAt;

  CommentModel({
    required this.id,
    required this.content,
    required this.userId,
    required this.userName,
    required this.userAvatar,
    required this.contentId,
    required this.type,
    this.likeCount = 0,
    required this.createdAt,
  });

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'content': content,
      'user_id': userId,
      'user_name': userName,
      'user_avatar': userAvatar,
      'content_id': contentId,
      'type': type.index,
      'like_count': likeCount,
      'created_at': createdAt.toIso8601String(),
    };
  }

  Map<String, dynamic> toMap() => toJson();

  factory CommentModel.fromMap(Map<String, dynamic> map) {
    return CommentModel(
      id: map['id'],
      content: map['content'],
      userId: map['user_id'],
      userName: map['user_name'],
      userAvatar: map['user_avatar'],
      contentId: map['content_id'],
      type: CommentType.values[map['type']],
      likeCount: map['like_count'],
      createdAt: DateTime.parse(map['created_at']),
    );
  }

  factory CommentModel.fromJson(String source) =>
      CommentModel.fromMap(json.decode(source));
}
